package com.miao.util.basic;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.XMLConfiguration;
import org.apache.log4j.Logger;

public class Config {

    private static final Logger logger = Logger.getLogger(Config.class);

    private static String CONFIG_FILENAME = "config.xml";

    private static XMLConfiguration config = null;

    static {
        try {
            config = new XMLConfiguration(CONFIG_FILENAME);
            System.out.println(config.getFile().getPath());
        } catch (ConfigurationException e) {
            System.out.println("xxxxxx");
        }
    }

    public static String[] getLocalProperties(String parent) {
        return config.getStringArray(parent);
    }

    public static int getIntLocalProperty(String name) {
        return config.getInt(name);
    }

    public static String getLocalProperty(String name) {
        return config.getString(name);
    }

    public static int getLocalProperty(String name, int defaultValue) {
        String value = getLocalProperty(name);
        if (value == null)
            return defaultValue;
        try {
            return Integer.parseInt(value);
        } catch (NumberFormatException e) {
            logger.error("getLocalProperty(String, int)", e);
        }
        return defaultValue;
    }

    public static String getConnectionProvider() {
        return getLocalProperty("database.connectionProvider.className");
    }

    public static Object getProperties(String name) {
        return config.getProperties(name);
    }

    public static void main(String[] args) {
        System.out.println(Config.getLocalProperty("fastdfs.domain"));
        System.out.println(getProperties("global"));
        System.out.println(getLocalProperty("redis.host"));
    }

}
